package com.ruoyi.web.controller.ai;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.ai.domain.PromotMsgUtils;
import com.ruoyi.ai.domain.vo.ChatCommonVo;
import com.ruoyi.ai.service.IMedicineQuestionService;
import com.ruoyi.ai.service.impl.AiServiceImpl;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class AiController extends BaseController {

    private final AiServiceImpl aiService;
    private final IMedicineQuestionService medicineQuestionService;

    @PostMapping("/chatStreamOnce")
    @Log(title = "chatStreamOnce", businessType = BusinessType.OTHER)
    public R<String> chatStreamOnce(@RequestBody ChatCommonVo chatCommonVo){
        return R.ok(aiService.chatStreamOnce(chatCommonVo));
    }

    @PostMapping("/medicinequestion")
    @Log(title = "medicinequestion", businessType = BusinessType.OTHER)
    public R<String> medicinequestion(@RequestBody Map<String,String> param){
        if(ObjectUtils.isEmpty(param)){
            return R.fail("参数为空");
        }
        String content="";
        for(Map.Entry<String,String> item:param.entrySet()){
            content+=item.getKey()+"答:"+item.getValue()+"。";
        }
        ChatCommonVo chatCommonVo=new ChatCommonVo();
        chatCommonVo.setModel("kimi");
        chatCommonVo.setMsgType("report");
        chatCommonVo.setContent(content);
        String aiResponse = aiService.chatStreamOnce(chatCommonVo);
        
        // 保存问诊记录
        medicineQuestionService.saveMedicineQuestion(param, aiResponse, chatCommonVo.getModel(), chatCommonVo.getMsgType());
        
        return R.ok(aiResponse);
    }

}
